﻿var popupStatus = 0;
//centering popup  
function centerPopup() {
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $('.add-news').height();
    var popupWidth = $('.add-news').width();
    $('.add-news').css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
}

function loadPopup() {
    if (popupStatus == 0) {
        $(".background-popup").css({
            "opacity": "0.7"
        });
        $('.background-popup').fadeIn("slow");
        $('.add-news').fadeIn("slow");
        popupStatus = 1;
    }
}

function disablePopup() {
    if (popupStatus == 1) {
        $(".background-popup").fadeOut("slow", function () {
            $(this).hide();
        });
        $('.add-news').fadeOut("slow", function () {
            $(this).hide();
        });  
        popupStatus = 0;
    }
}

$(document).keypress(function (e) {
    if (e.keyCode == 27 & popupStatus == 1) {
        disablePopup();
    }
});

$(function () {
    $('.plus-icon').click(function () {
        centerPopup();
        loadPopup();
        $('.form-popup textarea').val('');
        $('.form-popup input.required').each(function () {
            $(this).val('');
            $(this).removeClass('required-reminder');
            $(this).parent().children('.required-reminder-text').children('span').hide();
        });
    });

    $('form').submit(function () {
        var validated = true;
        $('.form-popup input.required').each(function () {
            if ($(this).val().length == 0) {
                validated = false;
                $(this).addClass('required-reminder');
                $(this).parent().children('.required-reminder-text').children('span').show();
            }
            else {
                $(this).removeClass('required-reminder');
                $(this).parent().children('.required-reminder-text').children('span').hide();
            }
        });
        return validated;
    });

    $('.close-icon').click(function () {
        disablePopup();
    });
});